package com.apache.bid.service.impl;

import java.util.Date;
import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.entity.BidOrg;
import com.apache.bid.manager.BidOrgManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.tools.DateUtils;

public class BidOrgManagerImpl implements BidOrgManager {

	protected IDao bidOrgDao;
	protected final String entityName = "com.apache.bid.entity.BidOrg";

	public void setBidOrgDao(IDao bidOrgDao) {
		this.bidOrgDao = bidOrgDao;
	}

	public String saveInfo(ParamsVo<BidOrg> vo) throws BusinessException {
		BidOrg bidOrg = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidOrg_"+infoId;
		if (Validator.isNull(bidOrg.getOrgId()))
			bidOrg.setOrgId(infoId);
		bidOrg.setCreateTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
		MethodParam param = new MethodParam("BidOrg", cacheKey, "", entityName);
		param.setVaule(bidOrg);
		if (bidOrgDao.insert(param)) {
			if (Constant.getInstance().ifWriteDatabase()) {
				List<String> names = BidCacheFactory.getInstance().getBidOrgEnames();
				names.add(bidOrg.getOrgEname());
				BidCacheFactory.getInstance().saveBidOrgEnames(names);
			} else {
				param.setParams("ifSave", "true");
				vo.setParams("methodCode", "orgCacheTask");
			}
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidOrg> vo) throws BusinessException {
		BidOrg bidOrg = vo.getObj();
		if (Validator.isNotNull(bidOrg.getOrgId())) {
			bidOrg.setModifyTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());//2016-03-21
			String cacheKey = "";
			//String cacheKey="bidOrg_"+bidOrg.getOrgId();
			MethodParam param = new MethodParam("BidOrg", cacheKey, "", entityName);
			param.setVaule(bidOrg);
			return bidOrgDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidOrg> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidOrg_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		BidOrg info = (BidOrg) bidOrgDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		boolean flag = false;
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("BidOrg");
			flag = bidOrgDao.edit(param);
		} else {
			param.setParams("orgId", infoId);
			param.setDelete(true);
			flag = bidOrgDao.delete(param);
		}
		if (flag) {
			if (Constant.getInstance().ifWriteDatabase()) {
				List<String> names = BidCacheFactory.getInstance().getBidOrgEnames();
				names.remove(info.getOrgEname());
				BidCacheFactory.getInstance().saveBidOrgEnames(names);
			} else {
				param.setParams("ifSave", "false");
				param.setParams("orgEname", info.getOrgEname());
				vo.setParams("methodCode", "orgCacheTask");
			}
		}
		return flag;
	}

	public Object getInfoById(ParamsVo<BidOrg> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidOrg_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidOrgDao.selectById(param);
	}

	public Object execute(ParamsVo<BidOrg> vo) {
		String key = vo.getKey();
		if ("findOrgEnames".equals(key)) {//获取有效的组织方标识列表
			return findOrgEnames();
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	private List findOrgEnames() {
		MethodParam param = new MethodParam("ByOrgEname", "", "", entityName);
		param.setResultCache(false);
		param.setParams("nowTime", DateUtils.format(new Date(), "yyyy-MM-dd"));
		return bidOrgDao.select(param);
	}

	public Page getPageInfo(ParamsVo<BidOrg> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		Page page = bidOrgDao.pageSelect(param);
		return page;
	}

	public List<BidOrg> getList(ParamsVo<BidOrg> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		return bidOrgDao.select(param);
	}

	public long countInfo(ParamsVo<BidOrg> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidOrgDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidOrg> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidOrgDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		param.setResultCache(false);
		BidOrg bidOrg = vo.getObj();
		if (Validator.isNotNull(bidOrg.getOrgEname())) {
			sb.append(" and orgEname = :orgEname");
			param.setParams("orgEname", bidOrg.getOrgEname());
		}
		if (Validator.isNotNull(bidOrg.getOrgCname())) {
			sb.append(" and orgCname like '%" + bidOrg.getOrgCname() + "%' ");
			param.setParams("orgCname", " like '%" + bidOrg.getOrgCname() + "%' ");
		}
		if (Validator.isNotNull(bidOrg.getOrgStatus())) {
			sb.append(" and orgStatus = :orgStatus");
			param.setParams("orgStatus", bidOrg.getOrgStatus());
		}
		if (Validator.isNotNull(bidOrg.getEndDate())) {
			sb.append(" and endDate = :endDate");
			param.setParams("endDate", bidOrg.getEndDate());
		}
		if (Validator.isNotNull(bidOrg.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", bidOrg.getCreateTime());
		}
		if (Validator.isNotNull(bidOrg.getModifyTime())) {
			sb.append(" and modifyTime = :modifyTime");
			param.setParams("modifyTime", bidOrg.getModifyTime());
		}
		if (Validator.isNotNull(bidOrg.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidOrg.getSpare1());
		}
		if (Validator.isNotNull(bidOrg.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidOrg.getSpare2());
		}
		if (Validator.isNotNull(bidOrg.getSpare3())) {
			sb.append(" and spare3 = :spare3");
			param.setParams("spare3", bidOrg.getSpare3());
		}
		if (Validator.isNotNull(bidOrg.getSpare4())) {
			sb.append(" and spare4 = :spare4");
			param.setParams("spare4", bidOrg.getSpare4());
		}
		if (Validator.isNotNull(bidOrg.getSpare5())) {
			sb.append(" and spare5 = :spare5");
			param.setParams("spare5", bidOrg.getSpare5());
		}
		if (Validator.isNotNull(bidOrg.getChargeName())) {
			sb.append(" and chargeName = :chargeName");
			param.setParams("chargeName", bidOrg.getChargeName());
		}
		if (Validator.isNotNull(bidOrg.getEmail())) {
			sb.append(" and email = :email");
			param.setParams("email", bidOrg.getEmail());
		}
		if (Validator.isNotNull(bidOrg.getTel())) {
			sb.append(" and tel = :tel");
			param.setParams("tel", bidOrg.getTel());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
